package com.jxt.myjxtmanager.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.jxt.myjxtmanager.BaseActivity;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.entity.FeedbackBean;
import com.jxt.myjxtmanager.okHttp.Result;
import com.jxt.myjxtmanager.ui.inter.FeedbackInter;
import com.jxt.myjxtmanager.ui.presenter.FeedbackPresenter;
import com.jxt.myjxtmanager.utils.ToastUtil;

public class FeedBackActivity extends BaseActivity implements FeedbackInter<Result> {

    private Button bt1, bt2, bt3, bt_send;
    private EditText et_text;
    private boolean[] isclick;
    private long user_id;
    private FeedbackBean feedback;
    private FeedbackPresenter presenter;

    @Override
    protected int getLayoutId() {
        return R.layout.user_feedback_view;
    }

    @Override
    protected void eventBundle(Bundle bundle) {
        super.eventBundle(bundle);
        user_id = bundle.getLong("user_id");
    }

    @Override
    protected void initView() {
        setToolbarTitle("意见反馈");
        setToolbarNavigationIcon(R.drawable.ic_left_return);
        //setToolbarNavigationIcon(R.drawable.ic_left_return);
        bt1 = (Button) findViewById(R.id.feddback_bt_e1);
        bt2 = (Button) findViewById(R.id.feddback_bt_e2);
        bt3 = (Button) findViewById(R.id.feddback_bt_e3);
        bt_send = (Button) findViewById(R.id.feedback_bt_send);
        et_text = (EditText) this.findViewById(R.id.send_feedback_et_text);
        presenter = new FeedbackPresenter(this, this);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt_send.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        feedback = new FeedbackBean();
        isclick = new boolean[3];
        isclick[0] = false;
        isclick[1] = false;
        isclick[2] = false;
    }

    @Override
    public void onClickLeftMenu() {
        onBackPressed();
    }

    @Override
    protected void onClickView(int key) {
        super.onClickView(key);
        switch (key) {
            case R.id.feddback_bt_e1:
                // 反馈按钮1
                changeBtBg(0, bt1);
                break;
            case R.id.feddback_bt_e2:
                // 反馈按钮3
                changeBtBg(1, bt2);
                break;
            case R.id.feddback_bt_e3:
                // 反馈按钮3
                changeBtBg(2, bt3);
                break;
            case R.id.feedback_bt_send:
                // 提交反馈
                sendFeedback();
                break;
            default:
                break;
        }
    }

    /**
     * change button background follow state
     */
    public void changeBtBg(int num, Button b) {
        if (!isclick[num]) {
            isclick[num] = true;
            b.setTextColor(getResources().getColor(R.color.color_002651));
            b.setBackgroundResource(R.drawable.bg_circle_3db2bf);
        } else {
            isclick[num] = false;
            b.setTextColor(getResources().getColor(R.color.txt_gray_bb));
            b.setBackgroundResource(R.drawable.bg_button_color_enable);
        }
    }

    /**
     * 反馈信息表单校验
     */
    public boolean validateFeedback() {
        setFeedback();
        if (feedback == null) {
            return false;
        }
        if (TextUtils.isEmpty(feedback.theme)) {
            Toast.makeText(this, "反馈主题不能为空！", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(feedback.content)) {
            Toast.makeText(this, "反馈内容不能为空！", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    // 发送反馈信息
    public void setFeedback() {
        String content = et_text.getText().toString();
        String theme = null;
        if (isclick[0])
            theme = 1 + "";
        if (isclick[1])
            theme = (theme == null ? "" : theme + ",") + 3;
        if (isclick[2])
            theme = (theme == null ? "" : theme + ",") + 4;
        feedback = new FeedbackBean();
        feedback.user_id = Long.parseLong(String.valueOf(user_id));
        if (isStringOk(content))
            feedback.content = content;
        if (isStringOk(theme))
            feedback.theme = theme;
    }


    /**
     * 发送反馈信息
     */
    public void sendFeedback() {
        if (validateFeedback())
            presenter.getFeedback(user_id, feedback);
//            Api.sendFeedback(this, driver_id, feedback, new HttpResponseResult(
//                    this, "", HttpResponseResult.TOAST_ALL_SHOW) {
//                @Override
//                public void onSuccess(String response) {
//                    Toast.makeText(FeedBackActivity.this,
//                            getString(R.string.toast_send_feedback_success),
//                            Toast.LENGTH_SHORT).show();
//                    onBackPressed();
//                }
//            });
    }

    public boolean isStringOk(String str) {
        return (str == null || str.isEmpty() || str.equals("")) ? false : true;
    }

    @Override
    public void showToast(String msg) {
        ToastUtil.showToast(this, msg);
    }

    @Override
    public void resuleCarSuccess(Result resultBeans) {
        presenter.dismissDialog();
        ToastUtil.showToast(this, "谢谢您的宝贵意见");
        finish();
    }
}
